<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AK JOB</title>
    <link rel="icon" type="image/x-icon" href="../../../layuiadmin/style/res/favicon.png"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card" id="admin-form">

    </div>
</div>

<script type="text/html" id="formTmp">
    <div class="layui-card-body">
        <div class="layui-form" lay-filter="layuiadmin-form-admin" id="layuiadmin-form-admin"
             style="padding: 20px 30px 0 0;">
            <div class="layui-form-item">
                <label class="layui-form-label">登录名</label>
                <div class="layui-input-block">
                    <input type="text" name="loginName" lay-verify="required" placeholder="请输入登录名"
                           autocomplete="off" class="layui-input" lay-verType="tips" value="${info?info.loginName:''}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">昵称</label>
                <div class="layui-input-block">
                    <input type="text" name="nickName" lay-verify="required" placeholder="请输入昵称"
                           autocomplete="off" class="layui-input" lay-verType="tips" value="${info?info.nickName:''}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-block">
                    <select name="roleId" lay-verify="required" placeholder="请选择角色" lay-verType="tips">
                        <option value="">请选择</option>
                        {@each roles as r}
                        {@if info && info.roleId == r.id}
                        <option value="${r.id}" selected>${r.roleName}</option>
                        {@else}
                        <option value="${r.id}">${r.roleName}</option>
                        {@/if}
                        {@/each}
                    </select>
                </div>
            </div>
            {@if action == "add"}
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" lay-verify="required" placeholder="请输入密码"
                           autocomplete="off" class="layui-input" lay-verType="tips">
                </div>
            </div>
            {@/if}
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <input class="layui-btn layui-hide" type="button" lay-submit lay-filter="adminSubmit"
                           id="adminSubmit"
                           value="保存">
                </div>
            </div>
        </div>
    </div>
</script>
<script src="../../../layuiadmin/layui/layui.js"></script>
<script src="../../../layuiadmin/lib/juicer-min.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'roles', 'commonAdmin', 'auth', 'layer', 'AppHelper'], function () {
        var $ = layui.$
            , form = layui.form
            , commonAdmin = layui.commonAdmin
            , layer = layui.layer
            , roles = layui.roles
            , AppHelper = layui.AppHelper
            , LayerPage = AppHelper.getLayerPage()
        ;

        //解析当前路由信息
        var router = layui.router();

        var action = router.search.action;
        var id = router.search.id;
        //刷新表格
        var refreshParentTable = function () {
            parent.layui.table.reload("LAY-user-back-manage");
        }

        switch (action) {
            case "add":
                var loadIndex = layer.load(1);
                roles.list()
                    .always(function () {
                        layer.close(loadIndex);
                    })
                    .done(function (res) {
                        var data = res.data?res.data.records:[];
                        $("#admin-form").html(juicer("#formTmp", {action: "add", roles: data}));
                        form.render("select");
                        form.on("submit(adminSubmit)", function (obj) {
                            var field = obj.field;
                            //保存
                            var layerPageLoad = LayerPage.load();
                            commonAdmin.methods.add(field)
                                .done(function (res) {
                                    if (res.code == "200") {
                                        layerPageLoad.close();
                                        parent.layer.msg("保存成功！", {icon: 1}, function () {
                                        })
                                        refreshParentTable();
                                        LayerPage.close();
                                    }
                                })
                                .fail(function () {
                                    layerPageLoad.close();
                                });
                        })
                    })
                break;
            case "edit":
                var layerPageLoad = LayerPage.load();
                $.when(roles.list(), commonAdmin.methods.get(id))
                    .always(function () {
                        layerPageLoad.close();
                    })
                    .done(function (roleReq, adminReq) {
                        var roles = roleReq[0].data?roleReq[0].data.records:[];
                        var adminInfo = adminReq[0].data;

                        $("#admin-form").html(juicer("#formTmp", {action: "edit", roles: roles, info: adminInfo}));
                        form.render("select");

                        form.on("submit(adminSubmit)", function (obj) {
                            var field = obj.field;
                            field.id = id;
                            //保存
                            var layerPageLoad = LayerPage.load();
                            commonAdmin.methods.update(field).done(function (res) {
                                if (res.code == "200") {
                                    layerPageLoad.close();
                                    parent.layer.msg("修改成功！", {icon: 1}, function () {
                                    })
                                    refreshParentTable();
                                    LayerPage.close();
                                }
                            }).fail(function () {
                                layerPageLoad.close();
                            });
                        })
                    })
                break;
        }

    })
</script>
</body>
</html>